package com.pig4cloud.pig.module.controller.devices;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig4cloud.pig.common.core.util.OkHttpClientUtil;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.devices.DevicesMaterial;
import com.pig4cloud.pig.module.service.devices.IDevicesMaterialService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 设备的视频和图片素材Controller
 *
 * @author ruoyi
 * @date 2022-01-11
 */
@Validated
@Api(value = "设备的视频和图片素材控制器", tags = {"设备的视频和图片素材管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/device/material")
public class DevicesMaterialController {

    private final IDevicesMaterialService devicesMaterialService;

//    @Value("${ruoyi.imgUrl}")
//    private String imgUrl;

	@Value("${ruoyi.imgUrlOss}")
	private String imgUrlOss;
    /**
     * 查询设备的视频和图片素材列表
     */
    @ApiOperation("查询设备的视频和图片素材列表")
    @PreAuthorize("@pms.hasPermission('module:devicesMaterial:list')")
    @GetMapping("/list")
    public TableDataInfo<DevicesMaterial> list(@Validated DevicesMaterial devicesMaterial) {
        QueryWrapper<DevicesMaterial> qw = new QueryWrapper<>();
        if(devicesMaterial.getDeviceId() !=null && devicesMaterial.getDeviceId() > 0){
            qw.eq("device_id",devicesMaterial.getDeviceId());
        }
        TableDataInfo<DevicesMaterial> td = PageUtils.buildDataInfo(devicesMaterialService.page(PageUtils.buildPage(), qw));
        List<DevicesMaterial> list = td.getData();
        for (DevicesMaterial material : list){
            material.setImgPath( imgUrlOss + material.getImgUrl());
        }
        return td;
    }

    /**
     * 导出设备的视频和图片素材列表
     */
    @ApiOperation("导出设备的视频和图片素材列表")
    @PreAuthorize("@pms.hasPermission('module:devicesMaterial:export')")
    @SysLog("设备的视频和图片素材")
    @GetMapping("/export")
    public List export(@Validated DevicesMaterial devicesMaterial) {
        List<DevicesMaterial> list = devicesMaterialService.list();
        // ExcelUtil<DevicesMaterial> util = new // ExcelUtil<>(DevicesMaterial.class);
        return list;// util.exportExcel(list, "设备的视频和图片素材");
    }

    /**
     * 获取设备的视频和图片素材详细信息
     */
    @ApiOperation("获取设备的视频和图片素材详细信息")
    @PreAuthorize("@pms.hasPermission('module:devicesMaterial:query')")
    @GetMapping("/{id}")
    public R<DevicesMaterial> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        DevicesMaterial devicesMaterial = devicesMaterialService.getById(id);
        return R.ok(devicesMaterial);
    }


    /**
     * 获取设备的视频和图片素材详细信息
     */
    @ApiOperation("获取设备的视频和图片素材详细信息")
    @PreAuthorize("@pms.hasPermission('module:devicesMaterial:query')")
    @GetMapping("/synchro")
    public R<Void> getInfo() {

        OkHttpClientUtil.getInstance().getData("https://api.sdyuedou.com/ydapi/parameter/reset/1");
        String json = "{\"device_id\":0}";
        try {
            String str = OkHttpClientUtil.getInstance().postJson("https://api.sdyuedou.com/ydapi/device/material/synchro",json);
            System.out.println("同步设备广告： "+  str);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.ok();
    }


    /**
     * 新增设备的视频和图片素材
     */
    @ApiOperation("新增设备的视频和图片素材")
    @PreAuthorize("@pms.hasPermission('module:devicesMaterial:add')")
    @SysLog("设备的视频和图片素材")

    @PostMapping()
    public R<Void> add(@Valid @RequestBody DevicesMaterial devicesMaterial) {
        return R.okByRow(devicesMaterialService.save(devicesMaterial) ? 1 : 0);
    }

    /**
     * 修改设备的视频和图片素材
     */
    @ApiOperation("修改设备的视频和图片素材")
    @PreAuthorize("@pms.hasPermission('module:devicesMaterial:edit')")
    @SysLog("设备的视频和图片素材")

    @PutMapping()
    public R<Void> edit(@Valid @RequestBody DevicesMaterial devicesMaterial) {
        return R.okByRow(devicesMaterialService.updateById(devicesMaterial) ? 1 : 0);
    }

    /**
     * 删除设备的视频和图片素材
     */
    @ApiOperation("删除设备的视频和图片素材")
    @PreAuthorize("@pms.hasPermission('module:devicesMaterial:remove')")
    @SysLog("设备的视频和图片素材")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return R.okByRow(devicesMaterialService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
